package frame.options.simulation.results.sdn.graphical;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.components.links.link.Link;
import model.components.networks.sdn.SdnNetwork;
import model.components.nodes.comparator.NodeNumberComparator;
import model.components.nodes.node.Node;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import frame.options.simulation.results.ResultsFrame;

@SuppressWarnings("serial")
public class SdnNetworkViewersPanel extends JPanel implements ChangeListener
{
	private List< SdnNetwork > sdnNetworks;
	public List< SdnNetwork > getSdnNetworks(){ return sdnNetworks; }
	public void setSdnNetworks( List< SdnNetwork > sdnNetworks ){ this.sdnNetworks = sdnNetworks; }
	
	private ResultsFrame resultsFrame;
	public ResultsFrame getResultsFrame(){ return resultsFrame; }
	public void setResultsFrame( ResultsFrame resultsFrame ){ this.resultsFrame = resultsFrame; }
	
	private List< SdnNetworkViewer > sdnNetworkViewers;
	public List< SdnNetworkViewer > getSdnNetworkViewers(){ return sdnNetworkViewers; }
	public void setSdnNetworkViewers( List< SdnNetworkViewer > sdnNetworkViewers ){ this.sdnNetworkViewers = sdnNetworkViewers; }
	
	private JLabel label;
	public JLabel getLabel(){ return label; }
	public void setLabel( JLabel label ){ this.label = label; }
	
	private JSlider slider;
	public JSlider getSlider(){ return slider; }
	public void setSlider( JSlider slider ){ this.slider = slider; }
	
	private int sliderPreviousValue;
	public int getSliderPreviousValue(){ return sliderPreviousValue; }
	public void setSliderPreviousValue( int sliderPreviousValue ){ this.sliderPreviousValue = sliderPreviousValue; }
	
	public SdnNetworkViewersPanel( List< SdnNetwork > sdnNetworks, ResultsFrame resultsFrame )
	{
		this.setSdnNetworks( sdnNetworks );
		this.setResultsFrame( resultsFrame );
		
		this.setSdnNetworkViewers( this.sdnNetworkViewers( sdnNetworks ) );
		
		this.setLabel( new JLabel( sdnNetworks.get( 0 ).getName(), null, SwingConstants.CENTER ) );
		this.setSlider( this.slider( sdnNetworks.size() ) );
		this.setLayout( new BorderLayout() );
		this.add( this.getLabel(), BorderLayout.NORTH );
		this.add( this.getSdnNetworkViewers().get( 0 ), BorderLayout.CENTER );
		this.add( this.getSlider(), BorderLayout.SOUTH );
	}
	
	private List< SdnNetworkViewer > sdnNetworkViewers( List< SdnNetwork > sdnNetworks )
	{
		List< SdnNetworkViewer > sdnNetworkViewers = new ArrayList< SdnNetworkViewer >();
		
		for( SdnNetwork sdnNetwork: sdnNetworks )
		{
			CircleLayout< Node, Link > circleLayout = new CircleLayout< Node, Link >( sdnNetwork.getGraph() );
			circleLayout.setVertexOrder( new NodeNumberComparator() );
			
			SdnNetworkViewer sdnNetworkViewer = new SdnNetworkViewer( circleLayout, this.getResultsFrame() );
			sdnNetworkViewer.setBackground( Color.GRAY );
			
			sdnNetworkViewers.add( sdnNetworkViewer );
		}
		
		return sdnNetworkViewers;
	}
	
	private JSlider slider( int sdnNetworksNumber )
	{
		JSlider slider = new JSlider();
		slider.setPaintTicks( true );
		slider.setSnapToTicks( true );
		slider.setPaintTrack( true );
		slider.setPaintLabels( true );
		slider.setMinimum( 0 );
		slider.setMaximum( sdnNetworksNumber - 1 );
		slider.setMinorTickSpacing( 1 );
		slider.setMajorTickSpacing(	1 );
		slider.setValue( 0 );
		slider.addChangeListener( this );
		
		return slider;
	}
	
	@Override
	public void stateChanged( ChangeEvent changeEvent )
	{
		this.setSlidersValue( this.getSlider().getValue() );
	}
	
	public void putValueSliderss( int index )
	{
		this.setSlidersValue( index );
		
		this.getResultsFrame().getSdnNetworksTabbedPane().setSelectedIndex( 0 );
	}
	
	private void setSlidersValue( int index )
	{
		this.setSliderValue( index );
		
		int selectedIndex = this.getResultsFrame().getSdnNetworksViewersPanelsTabbedPane().getSelectedIndex();
		this.getResultsFrame().getSdnNetworksTablesPanelsTabbedPane().setSelectedIndex( selectedIndex );
		this.getResultsFrame().getSdnNetworksTablesPanels().get( selectedIndex ).setSliderValue( index );
	}
	
	public void setSliderValue( int index )
	{
		SdnNetwork sdnNetwork = this.getSdnNetworks().get( index );
		
		if( sdnNetwork.getTimeStamp() == -1 )
			this.getLabel().setText( sdnNetwork.getName() );
		else
			this.getLabel().setText( "Time: " + sdnNetwork.getTimeStamp() );
		
		this.remove( this.getSdnNetworkViewers().get( this.getSliderPreviousValue() ) );
		this.setSliderPreviousValue( index );
		this.add( this.getSdnNetworkViewers().get( index ), BorderLayout.CENTER );
		this.getSlider().setValue( index );
		this.repaint();
	}
	
}
